package com.useeinfo.oa.modules.maintaining.entity;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.useeinfo.framework.extend.entity.SuperClassEntity;
import com.useeinfo.framework.sugar.tools.CommonSugar;
import com.useeinfo.oa.modules.base.entity.Enterprise;
import org.apache.commons.lang.StringUtils;

import javax.persistence.*;

/**
 * 周期设置表
 */
@Entity
@Table(name="period_install")
public
class PeriodInstall extends SuperClassEntity<PeriodInstall> {
    private String name;//周期名称
    private String frequency;//间隔
    private String type;//频率
    private String allDate;//月份
    private String week;//选中的星期
    private String remark;//说明
    @Column(columnDefinition = "MEDIUMTEXT")
    private String time;//指定时间
//    private String day;//指定天数
    /**
     * 1.间隔 2.日期 3.星期
     */
    private String planType;

    @ManyToOne
    @JoinColumn(name = "enterprise_id")
    private Enterprise enterprise;


    @Override
    public JSONObject toJSONObject() {
        JSONObject jsonObject = super.toJSONObject();
        jsonObject.put("id", CommonSugar.getTypedDefault(getId(), 0L));
        jsonObject.put("name", CommonSugar.getTypedDefault(getName(), ""));
        jsonObject.put("frequency", CommonSugar.getTypedDefault(getFrequency(), ""));
        jsonObject.put("remark", CommonSugar.getTypedDefault(getRemark(), ""));
        jsonObject.put("type", CommonSugar.getTypedDefault(getType(), ""));
        jsonObject.put("allDate", CommonSugar.getTypedDefault(getAllDate(), ""));
        jsonObject.put("time", JSONArray.parse(getTime()));
        jsonObject.put("planType", CommonSugar.getTypedDefault(getPlanType(),""));
        jsonObject.put("enterpriseName", CommonSugar.getTypedDefault(getEnterprise().getEnterpriseName(),""));

//        jsonObject.put("day", CommonSugar.getTypedDefault(getDay(), ""));

        String week="";
        String weekdays="";
        if(StringUtils.isNotEmpty(getWeek())){
            for (String weekday:getWeek().split(",")) {
                if(weekday.equals('1')){
                    weekdays="星期一";

                }else if(weekday.equals('2')){
                    weekdays="星期二";

                }else if(weekday.equals('3')){
                    weekdays="星期三";

                }else if(weekday.equals('4')){
                    weekdays="星期四";

                }else if(weekday.equals('5')){
                    weekdays="星期五";

                }else if(weekday.equals('6')){
                    weekdays="星期六";

                }else if(weekday.equals('7')){
                    weekdays="星期日";

                }
                if(StringUtils.isNotEmpty(week)){
                    week=weekdays;
                }else{
                    week=week+","+weekdays;
                }
            }
        }
        jsonObject.put("week", JSONArray.parse(getWeek()));
        return  jsonObject;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getFrequency() {
        return frequency;
    }

    public void setFrequency(String frequency) {
        this.frequency = frequency;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getAllDate() {
        return allDate;
    }

    public void setAllDate(String allDate) {
        this.allDate = allDate;
    }

    public String getWeek() {
        return week;
    }

    public void setWeek(String week) {
        this.week = week;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public String getPlanType() {
        return planType;
    }

    public void setPlanType(String planType) {
        this.planType = planType;
    }

    public Enterprise getEnterprise() {
        return enterprise;
    }

    public void setEnterprise(Enterprise enterprise) {
        this.enterprise = enterprise;
    }
}
